<?php
namespace app\wxadmin\controller;

use think\Controller;
use think\Request;

class Index extends Controller
{

    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(empty(session('qywx_user'))||empty(session('cdb')))
        {
            jump('/wx/login');
        }
        if(empty(session('nav')))
        {
            $this->get_nav(input('get.app'));
        }

    }

    public function index()
    {
        //dump(session('qywx_user'));
        //dump(session('cdb'));
        $db=model('Hdb')->db();
        $this->assign('user',session('qywx_user'));
        $this->assign('nav_list',session('nav'));
        $this->assign('company',$db->name('company')->where('corpid',session('cdb')['corpid'])->find());
        $app=input('get.app');
        if($app=='')
        {
            $this->assign('now_app',session('nav')[0]['app']);
        }else{
            $is_use=false;
            foreach(session('nav') as $v)
            {
                if($app==$v['app'])
                {
                    $is_use=true;
                }
            }
            if($is_use==true)
            {
                $this->assign('now_app',$app);
            }else{
                $this->assign('now_app',session('nav')[0]['app']);
            }
        }
        return $this->fetch('templates/wxadmin/index.html');
    }

    public function main()
    {
        dump(session('qywx_user'));
        //dump(session('cdb'));
        return $this->fetch('templates/wxadmin/main.html');
    }

    function get_nav($app='')
    {
        $db=model('Hdb')->db();
        $nav_user=session('qywx_user')['agent'];

        foreach($nav_user as $v)
        {
            $agentid[]=$v['agentid'];
        }
        $app_list=$db->name('company_app')->where('corpid',session('cdb')['corpid'])->where('agentid','in',$agentid)->select();
        session('nav',$app_list);
    }

    function get_nav_list()
    {
        return $this->fetch('templates/wxadmin/'.input('app').'/nav.html');
    }
}
